package com.parkinglot.management.controller;

import com.parkinglot.management.repository.entity.Order;
import com.parkinglot.management.service.EnteredCarInfoService;
import com.parkinglot.management.service.OrderService;
import com.parkinglot.management.util.SaveImg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(tags = "订单管理")
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    EnteredCarInfoService enteredCarInfoService;

    @Autowired
    SaveImg saveImg;

    private  final SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

    @ApiOperation("得到所有的订单信息")
    @GetMapping
    public List<Order> getAllOrders(){
        return orderService.getAllOrders();
    }

    @ApiOperation("添加订单信息")
    @PostMapping
    public Order addOrder(@RequestBody MultipartFile file) throws ParseException {
        String leavedTime = df.format(new Date());
        String spliceImgPath = saveImg.saveUploadedImg(file,leavedTime);
        return orderService.addOrder(spliceImgPath,leavedTime);
    }

    @ApiOperation("根据车牌号获取订单信息")
    @GetMapping("/{carNum}")
    public List<Order> getOrderByNum(@PathVariable String carNum){
        return orderService.getOrderByNum(carNum);
    }

    @ApiOperation("删除订单")
    @DeleteMapping
    public void deleteOrder(Order order){
        orderService.deleteOrder(order);
    }

    @ApiOperation("修改订单状态")
    @GetMapping("/change")
    public void changeOrderStatus(HttpServletRequest request) {
        String productCode = request.getParameter("productCode");
        System.out.println(productCode);
    }
}
